import {Observer} from "@/utils/observer/Observer";
import {UserRoomApi} from "@/api/user_api";

/**
 * TODO 弃用了
 */
class RoomSchedulingObserverBiz extends Observer{
  constructor(self, deviceInfo = {}, item) {
    super();
    // 外部组件
    this.catcheInfo = self;
    // 设备信息
    this.deviceInfo = deviceInfo;
    // 当前组件信息
    this.currentInfo = item || {};
  }

 async init() {
   await this.roomReloadScheduling()
 }
  /**
   * 消息监听入口
   * @param bizType
   * @param msgContent
   */
  async update(bizType, msgContent) {
    // alert(bizType)
    let self = this.catcheInfo;
    try {
      switch (bizType) {
        case "doctorLogin":
          await this.doctorLogin(msgContent);
          break;
        case "doctorLogout":
          await this.doctorLogout(msgContent);
          break;
        case "refreshMsg":
          await this.refreshMsg(msgContent);
          break;
          // 刷新排班信息
        case "Qcss_Refresh_Config_Msg":
          await this.refreshMsg(msgContent);
          break;
      }
    } catch (e) {
      console.error("消息数据加载失败：" + msgContent)
      self.handleException("sdkBizServices", "安卓消息消费异常 -- " + bizType + " -- >", JSON.stringify(e.stack));
    }

  }
  /**
   * 医生登录
   */
  async doctorLogin() {
    await this.roomReloadScheduling();
  }


  /**
   * 医生退出
   * @param obj
   */
  async doctorLogout(obj) {
    await this.roomReloadScheduling(obj);
  }

  /**
   * 刷新消息
   * @param obj
   */
  async refreshMsg(obj) {
    await this.roomReloadScheduling(obj);
  }

  /**
   * 加载排班信息
   */
  async roomReloadScheduling() {
    // 重置排班
    let self = this.catcheInfo;
    self.workerSchedulingJson = {};
    // 重新加载排班
    await this.roomLoadScheduling();
  }

  /**
   * 查询排班信息
   */
  async roomLoadScheduling() {
    // 查询排班
    let workerIdListArr = [];
    let self = this.catcheInfo;
    self.roomScreenData.workerInfoList.forEach((item) => {
      if (item.workerId !== '') {
        if (!self.workerSchedulingJson[item.workerId] || self.workerSchedulingJson[item.workerId].length === 0) {
          workerIdListArr.push(item.workerId);
        }
      }
    })
    await this.roomGetScheduling(workerIdListArr);
  }

  /**
   * 定制排班
   * @param docIdArr
   * @returns {Promise<void>}
   */
  async roomGetScheduling(docIdArr) {
    if(this.currentInfo.screenDataType.indexOf("scheduling-customize")){
      if (!!docIdArr && !!docIdArr.length) {
        let self = this.catcheInfo;
        for (let i = 0; i < docIdArr.length; i++) {
          let self = this;
          const formData = {
            workerIds: docIdArr[i]
          };
          const {data, code} = await UserRoomApi.getSchedulingInfo(formData);
          if (code === 200) {
            // 排班缓存
            self.workerSchedulingJson[docIdArr[i]] = data || [];
            // 刷新数据
            self.eventBus.$emit('loadViewData',"");
          }
        }
      }
    }

  }



}

export {RoomSchedulingObserverBiz};
